﻿#region License Information

/* •——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————•
   | Copyright Aptillon, Inc.                                                                                        |
   | This file is part of the CKS:API project - cksapi.codeplex.com                                                       |
   | Usage, modification and distribution subject to the license terms available here: http://cksapi.codeplex.com/license |
   |                                                                                                                      |
   | Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,             |
   | either express or implied.                                                                                           |
   |                                                                                                                      |
   | THIS MESSAGE MAY NOT BE LEGALLY REMOVED                                                                              |
   •——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————• */

#endregion


using System.Diagnostics;
using System;
using System.Web;
using CKS.API.Utilities;
using Microsoft.SharePoint;
using CKS.API.Contracts;
using CKS.API.Logging;
using SPDisposeCheck;

namespace CKS.API.Exceptions
{
    /// <summary>
    /// Custom exception class for errors caused at runtime by an invalid parameter being passed to a method.  
    /// Inherits from <see cref="CKSException" /> 
    /// </summary>
    /// <remarks>
    /// 
    /// Modification History: NONE
    /// </remarks>
    [Serializable]
    public class CKSContractException : CKSException
    {

        public CKSContractException(string message) : base(message, null, CKSLoggingCategory.Contracts, Guid.NewGuid()) { }


        public CKSContractException(string parameterName, string contractName, object actualValue, object expectedValue)
            : base(
                string.Format(Resources.ContractFailed, parameterName, contractName, expectedValue, actualValue),
                null,
                CKSLoggingCategory.Contracts,
                Guid.NewGuid())
        {

        }

        [SPDisposeCheckIgnore(SPDisposeCheckID.SPDisposeCheckID_140, "False Positive - RootWeb")]
        internal CKSContractException(string parameterName, string contractName, object actualValue, object expectedValue, bool shortCircuit, CKSLogger logger)
            : base(
                 string.Format(Resources.ContractFailed, parameterName, contractName, expectedValue, actualValue),
                null,
                CKSLoggingCategory.Contracts,
                Guid.NewGuid())
        {
            if (shortCircuit)
            {
                logger.Write(string.Format(Resources.ContractFailed, parameterName, contractName, expectedValue, actualValue));
                if (
                    (Check.ThatAllAreNotNull(SPContext.Current, SPContext.Current.Site, SPContext.Current.Site.RootWeb, SPContext.Current.Site.RootWeb.ServerRelativeUrl))
                    && (Check.That(SPContext.Current.Site.RootWeb.ServerRelativeUrl.ValidateIsNotEqualTo(string.Empty)))
                    )
                {
                    string redirectPage = String.Format("{0}SiteAssets/CKS/{1}", SPContext.Current.Site.RootWeb.ServerRelativeUrl, Constants.ErrorPageName);
                    HttpContext.Current.Response.Redirect(redirectPage, true);
                }
            }
        }



    }
}